package com.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.eduservice.entity.EduChapter;
import com.edu.eduservice.entity.EduVideo;
import com.edu.eduservice.mapper.EduChapterMapper;
import com.edu.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.edu.eduservice.service.EduVideoService;
import com.edu.eduservice.vo.chapter.ChapterVo;
import com.edu.eduservice.vo.chapter.VideoVo;
import com.edu.eduservicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
   private EduVideoService videoService;


    /**
     * 根据课程id查询章节小节
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id--查询章节
        QueryWrapper<EduChapter> wrapper =new QueryWrapper();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);
        //根据课程id--查询小节
        QueryWrapper<EduVideo> wrapper1 =new QueryWrapper<EduVideo>();
        wrapper1.eq("course_id",courseId);
        List<EduVideo> eduVideos = videoService.list(wrapper1);
        //最终集合
        ArrayList<ChapterVo> findAllList = new ArrayList<>();
        //遍历 章节 封装
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter chapter = eduChapters.get(i);
            ChapterVo chapterVo =new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            findAllList.add(chapterVo);

            List<VideoVo> videoList = new ArrayList<>();
            //遍历 小节 封装
            for (int j = 0; j < eduVideos.size(); j++) {
                EduVideo eduVideo = eduVideos.get(j);
                VideoVo videoVo =new VideoVo();
                BeanUtils.copyProperties(eduVideo,videoVo);
                videoList.add(videoVo);
            }
            chapterVo.setChildren(videoList);
        }
        return findAllList;
    }


    /**
     * 删除章节
     * @param chapterId
     */
    @Override
    public boolean delChapter(String chapterId) {
        //根据章节id查询小节
        QueryWrapper<EduVideo> wrapper =new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //根据条件查询记录.返回记录
        int count = videoService.count(wrapper);
        if (count>0){//不能删除
            throw new GuliException(20001,"删除失败!章节中存在小节!");
        }else {
            int i = baseMapper.deleteById(chapterId);
                 return i>0;

        }
    }

    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper =new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }


}
